<?php
declare (strict_types=1);

namespace app\backend\controller\v1;

use app\common\model\Product as ProductModle;
use think\Request;
use app\backend\validate\ProductValidate;
use think\db\Query;

class Product extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $model = new ProductModle();
        $datas = $model
            ->where('status', 1)
            ->with(['productCate', 'productValue'])
            ->select()
            ->toArray();
        foreach ($datas as &$v) {
            $v['content'] = json_decode($v['content']);
        }
        return sucessMsg(200, '获取成功！', $datas);
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(ProductValidate::class)->scene('save')->check($postData);
            $image = request()->file('image');
            $big_image = request()->file('big_image');
            $desc=request()->file('desc');
            $content=request()->file('content');

            if (!empty($image)) {
                // 上传
                $savename1 = \think\facade\Filesystem::disk('public')->putFile('/uploads', $image);
                $postData['image'] = $savename1;
            }
            if (!empty($big_image)) {
                // 上传
                $savename2 = \think\facade\Filesystem::disk('public')->putFile('/uploads', $big_image);
                $postData['big_image'] = $savename2;
            }
            if (!empty($desc)){
                $savename3 = [];
                foreach($desc as $file){
                    $savename3[] = \think\facade\Filesystem::disk('public')->putFile('/uploads', $file);
                }
                $postData['desc']=implode(',',$savename3);
            }
            if (!empty($content)){
                $savename4 = [];
                foreach($desc as $file2){
                    $savename4[] = \think\facade\Filesystem::disk('public')->putFile('/uploads', $file2);
                }
                $postData['content']=implode(',',$savename4);
            }
            //dd($postData);
            $model = new ProductModle();
            if ($model->save($postData))
                return sucessMsg(200, '添加成功');
            return sucessMsg(422, '添加失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit()
    {
        //
        $model = new ProductModle();
        $id = input('get.id/d');
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(ProductValidate::class)->scene('update')->check($postData);
            $model = new ProductModle();
            $image = request()->file('image');
            $big_image = request()->file('big_image');
            $desc=request()->file('desc');
            $content=request()->file('content');

            if (!empty($image)) {
                // 上传
                $savename1 = \think\facade\Filesystem::disk('public')->putFile('/uploads', $image);
                $postData['image'] = $savename1;
            }
            if (!empty($big_image)) {
                // 上传
                $savename2 = \think\facade\Filesystem::disk('public')->putFile('/uploads', $big_image);
                $postData['big_image'] = $savename2;
            }
            if (!empty($desc)){
                $savename3 = [];
                foreach($desc as $file){
                    $savename3[] = \think\facade\Filesystem::disk('public')->putFile('/uploads', $file);
                }
                $postData['desc']=implode(',',$savename3);
            }
            if (!empty($content)){
                $savename4 = [];
                foreach($desc as $file2){
                    $savename4[] = \think\facade\Filesystem::disk('public')->putFile('/uploads', $file2);
                }
                $postData['content']=implode(',',$savename4);
            }
            $cate = $model->where('id', $postData['id'])->find();
            if ($cate->save($postData))
                return sucessMsg(200, '修改成功');
            return sucessMsg(422, '修改失败');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        $model = new ProductModle();
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(422, 'id不能为空');
        }
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        if ($data->save(['id' => $id, 'status' => -1]))
            return sucessMsg(200, '删除成功');
        return errMsg(422, '删除失败');
    }
}

